package org.example.struct.array;

/**
 * @author liyishan
 * @date 2023/8/4 10:28
 * @apiNote
 */

public class Test00643FindMaxAverage {
    public static void main(String[] args) {
        int[] nums = {1,12,-5,-6,50,3};
        double res = test(nums,4);
        System.out.println(res);
    }

    public static double test(int[] nums, int k) {
        int max = Integer.MIN_VALUE;
        int n = nums.length;
        for(int i=0;i<=n-k;i++){
            int sum = 0;
            for(int j = i;j<i+k;j++){
                sum += nums[j];
            }
            max = Math.max(max,sum);
        }
        return (double)max/k;
    }


    /**
     * 滑动窗口  区间为k的和 记录最大值
     * @param nums
     * @param k
     * @return
     */
    public static double test1(int[] nums,int k){
        int sum = 0;
        int n = nums.length;
        for (int i = 0; i < k; i++) {
            sum += nums[i];
        }
        int maxSum = sum;
        for (int i = k; i < n; i++) {
            sum = sum - nums[i - k] + nums[i];
            maxSum = Math.max(maxSum, sum);
        }
        return 1.0 * maxSum / k;
    }
}
